package com.stone.business.jc.action;

/**
 * <pre>
 * 周工作计划
 * @author 
 * </pre>
 */

import java.util.ArrayList;
import java.util.List;

import com.stone.business.jc.pojo.TPlanWeek;
import com.stone.business.jc.pojo.TPlanWeekDetail;
import com.stone.business.jc.service.JcCommonService;
import com.stone.para.IConstant;
import com.stone.sys.pojo.TOrgan;
import com.stone.sys.pojo.TUser;
import com.stone.utils.StringUtils;

import frm.ssh.action.BaseAction;
import frm.ssh.exception.AppException;

public class PlanWeekAction extends BaseAction {
	
	private static final long serialVersionUID = -4647594073681054487L;

	private JcCommonService myService;
	
	private TPlanWeek entity;

	private String id;
	
	private String ym;
	
	private String bz;
	
	private List<TPlanWeekDetail> planWeekList;
	
	private String isAdmin;
	
	private String detailid;
	
	/**
	 * 进入详细页面
	 * @return
	 * @throws AppException
	 */
	public String initDetail() throws AppException {
		
		getRole();

		// 修改进入
		if (!StringUtils.isNullOrBlank(id)) {
			this.entity = (TPlanWeek) myService.findById(Long.parseLong(id), TPlanWeek.class.getName());
			 //是否从个人待办事项进入班组管理列表 update by wangtao 2012-5-28
			 if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon19()) && ("grdbsx".equals(super.getSearchBean().getCon19()))){
				 TUser user = (TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER);
				 planWeekList = myService.findListByHql(" from TPlanWeekDetail where wcqk is null and fzrname  like '%"+user.getName()+ "%' and plantype = "+super.getSearchBean().getCon6()+" and planweekid = '" + id + "' order by bz ");
					
			 }else{
			planWeekList = myService.findListByHql(" from TPlanWeekDetail where plantype = "+super.getSearchBean().getCon6()+" and planweekid = '" + id + "' order by bz ");
			 }
			// 新建进入(不会用这里了)
		} else {
			this.entity = new TPlanWeek();
			planWeekList = new ArrayList<TPlanWeekDetail>();
			//是否从个人待办事项进入班组管理列表 update by wangtao 2012-11-28
			 if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon19()) && ("grdbsx".equals(super.getSearchBean().getCon19()))){
				 TUser user = (TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER);
				 planWeekList = myService.findListByHql(" from TPlanWeekDetail where wcqk is null and fzrname  like '%"+user.getName()+ "%' and plantype = "+super.getSearchBean().getCon6()+ " order by bz ");
					
			 //是否从班组待办事项进入管理工作工区列表 update by wangtao 2012-11-28
			 }else if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon19()) && ("bzdbsx".equals(super.getSearchBean().getCon19()))){
				 TUser user = (TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER);
				 planWeekList = myService.findListByHql(" from TPlanWeekDetail where sjwcdate is null and bz  like '%"+user.getOrgan().getId()+ "%' and plantype = "+super.getSearchBean().getCon6()+ " order by bz ");
					
			 }
			 else{
			planWeekList = myService.findListByHql(" from TPlanWeekDetail where plantype = "+super.getSearchBean().getCon6()+ " order by bz ");
			 }
		}

		return SUCCESS;
	}
		
	/**
	 * 保存
	 * @return
	 * @throws AppException
	 */
	public String save() throws AppException {
		
		myService.savePlanWeek(this.entity, this.planWeekList);
		super.setInfo("保存成功");
		getRole();
		return SUCCESS;
	}
	
	/**
	 * 删除
	 * @return
	 * @throws AppException
	 */
	public String delete() throws AppException {
		
		myService.deletePlanWeek(id);
		
		return "DEL-SUCCESS";
		
	}
	
	public void deletePlanDetail() throws Exception {
		myService.doDelAll(detailid, "TPlanWeekDetail");
        super.getResponse().getWriter().print("{msg:'删除成功'}");
	}
	
	private void getRole() {
		
		TUser user = (TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER);
		String roles = (String) super.getSession().getAttribute(IConstant.SESSION_ROLES);
		/*// 安全员
		if ("006".equals(user.getJxkhgw())) {
			
			this.bz = "安全员";
			
			// 技术员
		} else if ("004".equals(user.getJxkhgw())) {
			
			this.bz = "技术员";
			
			// 操作队队长
		} else if ("005".equals(user.getJxkhgw())) {
			
			this.bz = ((TOrgan) super.getSession().getAttribute(IConstant.SESSION_TORGAN)).getOrganName();
			
			// 维修班长
		} else if ("007".equals(user.getJxkhgw())) {
			
			this.bz = "维修班长";
			
		} else {
			this.bz = "";
		}*/
		//班组长
     if (roles.contains("ZBZ")) {
			
			this.bz =((TOrgan) super.getSession().getAttribute(IConstant.SESSION_TORGAN)).getOrganName();
			
			// 专工
		} else if (roles.contains("gqgl")) {
			
			this.bz = "gqgl";
			
			// 班组管理,组员
		} else if (roles.contains("bzgl")||roles.contains("group")) {
			
			this.bz = ((TOrgan) super.getSession().getAttribute(IConstant.SESSION_TORGAN)).getOrganName();
			
			
		}  else {
			this.bz = "";
		}
		
		isAdmin = (String) super.getSession().getAttribute(IConstant.SESSION_ISADMIN);
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setMyService(JcCommonService myService) {
		this.myService = myService;
	}

	public List<TPlanWeekDetail> getPlanWeekList() {
		return planWeekList;
	}

	public void setPlanWeekList(List<TPlanWeekDetail> planWeekList) {
		this.planWeekList = planWeekList;
	}

	public String getYm() {
		return ym;
	}

	public void setYm(String ym) {
		this.ym = ym;
	}

	public String getBz() {
		return bz;
	}

	public String getIsAdmin() {
		return isAdmin;
	}

	public void setIsAdmin(String isAdmin) {
		this.isAdmin = isAdmin;
	}

	public String getDetailid() {
		return detailid;
	}

	public void setDetailid(String detailid) {
		this.detailid = detailid;
	}

	public TPlanWeek getEntity() {
		return entity;
	}

	public void setEntity(TPlanWeek entity) {
		this.entity = entity;
	}
	
	

}
